package AgentTravel.Behaviours;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;

import org.joda.time.LocalDate;

import AgentTravel.MyOntology;
import AgentTravel.Behaviours.VisitorOrganiseVisitBehaviour;
import AgentTravel.LogicObjects.City;
import AgentTravel.LogicObjects.CityVisit;
import AgentTravel.LogicObjects.DayPlanPlanning;
import AgentTravel.LogicObjects.UserProfile;
import AgentTravel.Messages.NewDayPlanMessage;
import AgentTravel.Messages.PlanMyTripMessage;
import jade.core.AID;
import jade.core.behaviours.*;
import jade.lang.acl.*;

//...To bedzie zachowanie gdy przyjdzie wiadomosc od interfejsu o nowym planie wycieczki
public class VisitorReceivedRequest extends CyclicBehaviour
{
	
	public VisitorReceivedRequest(UserProfile myProfile,AID serverAgent)
	{
		this.myProfile=myProfile;
		this.serverAgent=serverAgent;
	}
	
	private AID serverAgent;
	private UserProfile myProfile;
	
	@Override
	public void action() 
	{
		MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.REQUEST);
		ACLMessage msg = myAgent.receive(mt);
		
		if (msg != null) 
		{	
			//Pytanie o profil
			if(msg.getContent().startsWith(MyOntology.requestProfileText))
			{
				System.out.println("VISITOR: Host chce ode mnie profil");
				ACLMessage reply=msg.createReply();
				try 
				{
					reply.setContentObject(myProfile);
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
				reply.setPerformative(ACLMessage.INFORM_REF);
				myAgent.send(reply);
				
			}
			else //Bedziemy planowac wycieczke
			{
				PlanMyTripMessage plan=null;
				
				try
				{
					plan=(PlanMyTripMessage)msg.getContentObject();
				}
				catch(UnreadableException ex)
				{
					System.out.println("Unreadable :(");
				}
				catch(Exception ex)
				{
					System.out.println("Nie wiadomo dlaczego nie dalo sie odczytac wiadomosci");
				}
				
				if(plan!=null)
				{
					//TODO: haslo, password
					
					//TODO: tutaj mielismy komiwojazera zrobic, na razie kolejnosc jest byle jaka
					System.out.println("VISITOR: Odebralem PlanMyTripMessage");
					
					int dayOffset=0;
					for(CityVisit c : plan.getTripPlan())
					{
						List<LocalDate> days=new LinkedList<LocalDate>();
						for(int i=0;i< c.getDaysToStay();i++)
						{
							LocalDate day = new LocalDate(plan.getTripStartDate());
							days.add(day.plusDays(dayOffset));
							dayOffset++;
							
							//info dla serwera ze planuje
							//TODO: niekoniecznie, ale w ogole powinno sie jeszcze poczekac na potwierdzenie
							
							ACLMessage toServ=new ACLMessage(ACLMessage.PROPAGATE);
							toServ.addReceiver(serverAgent);
							try {
								toServ.setContentObject(new NewDayPlanMessage(day, new DayPlanPlanning(c.getCityName())));
								System.out.println("VISITOR: info o planowaniu dla serwera");
								myAgent.send(toServ);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						
						}
						
						myAgent.addBehaviour(new VisitorOrganiseVisitBehaviour(City.Cities.get(c.getCityName()),
								days,plan.getPreferences(),myProfile));
					}
				}
			}
		}
		else 
		{
			block();
		}
	}

}
